package com.ewili.x.special.service.data.impl;

import com.ewili.framework.utils.ObjectHttpClientUtils;
import com.ewili.x.special.dao.inter.StockDao;
import com.ewili.x.special.entity.Stock;
import com.ewili.x.special.service.data.inter.ValueService;
import com.ewili.x.special.task.thread.inter.ValueThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wonder
 */
@Slf4j
@Service
public class ValueServiceImpl implements ValueService {

    @Autowired
    private StockDao stockDao;

    @Autowired
    private ValueThreadTask valueThreadTask;

    @Override
    public void spideValue() {
        List<Stock> stocks = stockDao.findAll();
        for (Stock stock : stocks) {
            this.spide(stock);
        }
    }

    public void spide(Stock stock) {
        String baseUrl = "http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=GZFX_GGZB&token=894050c76af8597a853f5b408b759f5d&st=TRADEDATE&sr=-1&p=1&ps=100&js=var%20BVjHgvpO={pages:(tp),data:(x),font:(font)}&filter=(SECURITYCODE='" + stock.getCode() + "')&rt=52100197";

        Long page = ObjectHttpClientUtils.getEastMoneyJsonPages(baseUrl);

        if (page ==null || page == 0) {
            return;
        }
        for (int i = 1; i <= page ; i++) {
            String url = "http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=GZFX_GGZB&token=894050c76af8597a853f5b408b759f5d&st=TRADEDATE&sr=-1&p="+i+"&ps=100&js=var%20BVjHgvpO={pages:(tp),data:(x),font:(font)}&filter=(SECURITYCODE='" + stock.getCode() + "')&rt=52100197";
            valueThreadTask.spideValue(url, stock.getCode());
        }
    }
}
